<?php
namespace Common\Model;

use Think\Model\RelationModel;

class SysUserModel extends RelationModel
{
    protected $_link = array(
        'numbers' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'SysNumber',
            'foreign_key' => 'user_id',
        ),
    );

    public function do_login($account, $password)
    {
//        $model = M('sys_user');
        $user = $this->relation(true)->where("account='%s' and password='%s'", $account, md5_encrypt($password))->find();
        if ($user) {
            session('user_auth', $user);

            $user_menu = M('view_user_menu');
            $user_menu_list = $user_menu->where("state>=0 and user_id=%d", $user["id"])->order('order_field asc')->select();
            $data = list_to_menu_tree($user_menu_list);
            session('user_menu_list', $data);
            return true;
        }
        return false;
    }

    public function update_name($userid, $name)
    {
        $result = $this->where('id=%d', $userid)->setField('name', $name);
        return $result;
    }

    public function update_password($userid, $oldpassword, $password)
    {
        $condition['id'] = array('eq', $userid);
        $condition['password'] = array('eq', fc_md5($oldpassword));
        $condition['state'] = array('eq', 0);
        $row = $this->where($condition)->field('id,phone,name,idcard,qq')->find();
        if ($row) {
            $result = $this->where('id=%d', $userid)->setField('password', fc_md5($password));
            if ($result) {
                return true;
            }
        }

        return false;
    }

    public function get($id)
    {
        $condition = array();
        $condition['id'] = array('eq', $id);
        $condition['state'] = array('eq', 0);
//        $model = $this->field(true)->join('mem_user_annex on mem_user_annex.code=mem_user.photoCode')->where($condition)
//            ->field('mem_user.id,mem_user.name,mem_user.truename,mem_user.phone,mem_user.idcard,mem_user.qq,mem_user.isphoneverify,mem_user.aptitude,mem_user.jobid,
//                    mem_user_annex.savepath savepath,mem_user_annex.savename savename')->find();
        $model = $this->field(true)->where($condition)
            ->field('mem_user.id,mem_user.name,mem_user.nick_name,mem_user.phone,mem_user.idcard,mem_user.qq,mem_user.addtime,mem_user.last_logintime')->find();
        return $model;
    }

    public function get_user_coupons($openid)
    {
        $model = M('mem_user_coupon');
        $condition = array();
        $condition['openid'] = array('eq', $openid);
        $condition['state'] = array('eq', 0);

        $list = $model->field(true)->where($condition)->select();
        return $list;
    }

    //获取某用户参与的某活动
    public function get_activity_by_acopenid($openid, $couponid)
    {
        $model = M('mem_user_coupon_activity');
        $condition = array();
        $condition['openid'] = array('eq', $openid);
        $condition['user_coupon_id'] = array('eq', $couponid);
        $condition['state'] = array('eq', 0);

        $result = $model->where($condition)->find();
        return $result;
    }

    public function add_user_coupons_activity($openid, $couponid, $discount, $essay = '')
    {
        $model = M('mem_user_coupon_activity');
        $data['ac_user_openid'] = $openid;
        $data['user_coupon_id'] = $couponid;
        $data['discount'] = $discount;
        $data['essay'] = $essay;

        $result = $model->add($data);
        if ($result) {
            return true;
        }
    }


//    private function assembleCondition($name, $state)
//    {
//        $condition = array();
//        if (!empty($key)) {
//            $key_param['name'] = array('like', '%' . $name . '%');
//        }
//
//        if (!empty($state)) {
//            $condition['state'] = $state;
//        } else {
//            $condition['state'] = array('neq', -1);
//        }
//
//        return $condition;
//    }

}